package com.mfsoft.camera;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.example.camerafull.Bitmap_with_ShadowLayer;
import com.example.camerafull.Che_do_anh;
import com.example.camerafull.Combine_bitmap;
import com.example.camerafull.Compositing_photo;
import com.example.camerafull.Draw_text_on_bitmap;
import com.example.camerafull.Merge_two_image;
import com.example.camerafull.Tao_Khung_Anh;
import com.mfsoft.model.UserActions;
import com.mfsoft.net.MyFunctions_THI;

public class MainActivity extends SherlockActivity implements OnClickListener {
	Button btn_Merge_two_image, btn_Bitmap_with_ShadowLayer, btn_Tao_Khung_Anh,
			btn_Draw_text_on_bitmap, btn_Combine_bitmap, btn_Che_do_anh,
			btn_Ghep_anh, btn_Open_Image,btn;
	private ActionBar mActionBar;
	@SuppressWarnings("unused")
	private ImageView ivLogo;
	private ImageView ivLogin;
	private CameraApp instance;
	private UserActions userActions;
	private ProgressBar pb_loadAvatar;
	private SharedPreferences preferences;
	
	MyFunctions_THI myFunctions_THI;
	String prefname="my_data";
	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// get action bar
		
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayShowHomeEnabled(false);
		mActionBar.setDisplayShowTitleEnabled(false);
		instance = (CameraApp) getApplicationContext();
		preferences = instance.cameraModel.getSharedPreferences();
		userActions = new UserActions(this, this);
		getControls();
		eventHandle();
		//
		khoitao();

	}

	// test svn
	// hangle events here
	private void eventHandle() {
		ivLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {

				preferences = getSharedPreferences(prefname,context.MODE_PRIVATE);		
			    String username =preferences.getString("username","");
			    if(username.equalsIgnoreCase("")||username.length()==0)
			    {
			    	Intent i = new Intent(getApplicationContext(),LoginedActivity.class);
					startActivityForResult(i, 10);
			    }
				else
				{
					Intent i = new Intent(getApplicationContext(),MyProfileActivity.class);
					startActivity(i);
					finish();
				}
			}
		});
	}
	
	// mapping views here
	private void getControls() {
		final String avatar = preferences.getString(UserActions.AVATAR, "");
		View custom = LayoutInflater.from(MainActivity.this).inflate(
				R.layout.customactionbar, null);
		ivLogo = (ImageView) custom
				.findViewById(R.id.menuViewButtonLogoactionbar);
		ivLogin = (ImageView) custom.findViewById(R.id.imageViewLoginactionbar);
		pb_loadAvatar = (ProgressBar) custom.findViewById(R.id.pb_loadAvatar);
		if (!avatar.equals("")) {
			pb_loadAvatar.setVisibility(View.VISIBLE);
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					final Bitmap avatarBitmap = userActions
							.getAvatarSocial(avatar);
					ivLogin.post(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							ivLogin.setImageBitmap(avatarBitmap);
							pb_loadAvatar.setVisibility(View.GONE);
						}
					});
				}
			}).start();
		}
		mActionBar.setCustomView(custom);
		mActionBar.setDisplayShowCustomEnabled(true);
		mActionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));
	}

	public void khoitao() {
		btn_Merge_two_image = (Button) findViewById(R.id.btn_Merge_two_image);
		btn_Merge_two_image.setOnClickListener(this);
		btn_Bitmap_with_ShadowLayer = (Button) findViewById(R.id.btn_Bitmap_with_ShadowLayer);
		btn_Bitmap_with_ShadowLayer.setOnClickListener(this);
		btn_Tao_Khung_Anh = (Button) findViewById(R.id.btn_Tao_Khung_Anh);
		btn_Tao_Khung_Anh.setOnClickListener(this);
		btn_Draw_text_on_bitmap = (Button) findViewById(R.id.btn_Draw_text_on_bitmap);
		btn_Draw_text_on_bitmap.setOnClickListener(this);
		btn_Combine_bitmap = (Button) findViewById(R.id.btn_Combine_bitmap);
		btn_Combine_bitmap.setOnClickListener(this);
		btn_Che_do_anh = (Button) findViewById(R.id.btn_Che_do_anh);
		btn_Che_do_anh.setOnClickListener(this);
		btn_Ghep_anh = (Button) findViewById(R.id.btn_Ghep_anh);
		btn_Ghep_anh.setOnClickListener(this);
		btn_Open_Image = (Button) findViewById(R.id.btn_Open_Image);
		btn_Open_Image.setOnClickListener(this);
		btn = (Button)findViewById(R.id.btnthunghiem);
		btn.setOnClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 10 && resultCode == 1 && data.hasExtra("user")) {
			if (data.getStringExtra("user").equalsIgnoreCase("success")) {
				ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_loading);
				RelativeLayout layoutContent = (RelativeLayout) findViewById(R.id.layoutContent);
				layoutContent.setVisibility(View.VISIBLE);
				progressBar.setVisibility(View.GONE);
				ivLogin.setImageResource(R.drawable.logined);
				String username = preferences.getString(UserActions.NICKNAME,
						"");
				Toast.makeText(getApplicationContext(),
						getString(R.string.welcome_user, username),
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btn_Merge_two_image:
			Intent in = new Intent(MainActivity.this, Merge_two_image.class);
			startActivity(in);
			break;
		case R.id.btn_Bitmap_with_ShadowLayer:
			Intent in1 = new Intent(MainActivity.this,
					Bitmap_with_ShadowLayer.class);
			startActivity(in1);
			break;
		case R.id.btn_Tao_Khung_Anh:
			Intent in2 = new Intent(MainActivity.this, Tao_Khung_Anh.class);
			startActivity(in2);
			break;
		case R.id.btn_Draw_text_on_bitmap:
			Intent in3 = new Intent(MainActivity.this,
					Draw_text_on_bitmap.class);
			startActivity(in3);
			break;
		case R.id.btn_Combine_bitmap:
			Intent in4 = new Intent(MainActivity.this, Combine_bitmap.class);
			startActivity(in4);
			break;
		case R.id.btn_Che_do_anh:
			Intent in5 = new Intent(MainActivity.this, Che_do_anh.class);
			startActivity(in5);
			break;
		case R.id.btn_Ghep_anh:
			Intent in6 = new Intent(MainActivity.this, Compositing_photo.class);
			startActivity(in6);
			break;
		case R.id.btn_Open_Image:
			Intent in7 = new Intent(MainActivity.this, OpenImageActivity.class);
			startActivity(in7);
			break;
		case R.id.btnthunghiem:
			Intent in8 = new Intent(MainActivity.this, MyProfileActivity.class);
			startActivity(in8);
			break;
			
		}

	}

}
